﻿using CefSharp;
using CPPEI.Coolzon.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using UI = CPPEI.Coolzon.UI;

namespace Sunboxsoft.ChanceData.Partlib.Common
{
    /// <summary>
    /// WebView.xaml 的交互逻辑
    /// </summary>
    public partial class WebView : ControlPart
    {
        private static CefSettings s_settings;

        private string _url;
        /// <summary>
        /// 网页地址
        /// </summary>
        //[Category("Other")]
        //[DisplayName("Url地址")]
        //[Description("Url")]
        //[Editor(typeof(UI.XProperty.Editors.XStringEditor), typeof(UI.XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Other, "Url", "Url地址")]
        public string Url
        {
            get
            {
                return _url;
            }
            set
            {
                if (_url != value)
                {
                    _url = value;
                    ReLoadPage();
                }
            }
        }

        private double? _zoomLevel = null;
        [XProperty(XPropertyCategory.Other, "ZoomLevel", "ZoomLevel")]
        public double? ZoomLevel
        {
            get
            {
                return _zoomLevel;
            }
            set
            {
                _zoomLevel = value;
            }
        }

        private CefSharp.Wpf.ChromiumWebBrowser _webView;
        public WebView()
        {
            InitializeComponent();

            if (s_settings == null)
            {
                s_settings = new CefSettings();
                //s_settings.SetOffScreenRenderingBestPerformanceArgs();
                Cef.Initialize(s_settings, true, false);
            }
            _webView = new CefSharp.Wpf.ChromiumWebBrowser();
            _webView.Background = Brushes.Black;
            
            this.Content = _webView;
            _webView.FrameLoadEnd += _webView_FrameLoadEnd;
        }

        void _webView_FrameLoadEnd(object sender, FrameLoadEndEventArgs e)
        {
            try
            {
                if (ZoomLevel != null)
                {
                    this.Dispatcher.BeginInvoke(new Action(() =>
                    {
                        _webView.SetZoomLevel(ZoomLevel.Value);
                    }));
                }
            }
            catch (Exception ex)
            {
            }
        }

        protected override void OnActive(PartContext context)
        {
            if(IsFirstActive)
            {
                if (!string.IsNullOrEmpty(Url))
                {
                    _webView.RegisterJsObject("CZScene", this.Scene.GetScriptObj());
                    if (Url.ToUpper().StartsWith("HTTP://")
                        || Url.ToUpper().StartsWith("HTTPS://"))
                    {
                        _webView.Address = Url;
                    }
                    else
                    {
                        _webView.Address = SolutionResourceManager.GetAbsoluteFilePath(Url);// _webView.Load(SolutionResourceManager.GetAbsoluteFilePath(Url));
                    }
                }
            }
            base.OnActive(context);
        }

        protected override void OnDestory()
        {
            _webView.Dispose();
            base.OnDestory();
        }

        public override void RaiseScriptNetEvent(string eventName, string dataString)
        {
            try
            {
                _webView.ExecuteScriptAsync("jzcode.event.callback", eventName, dataString);
            }
            catch (Exception ex)
            {
                _webView.ExecuteScriptAsync("civs.event.callback", eventName, dataString);
            }
        }

        // 重新加载页面
        private void ReLoadPage()
        {
            if (!string.IsNullOrEmpty(Url))
            {
                //_webView.RegisterJsObject("CZScene", this.Scene.GetScriptObj());
                if (Url.ToUpper().StartsWith("HTTP://")
                    || Url.ToUpper().StartsWith("HTTPS://"))
                {
                    _webView.Load(Url);
                }
                else
                {
                    _webView.Load(SolutionResourceManager.GetAbsoluteFilePath(Url));
                }
            }
        }
    }
}
